<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Number List Attribute - one more, with style</title>
<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Spirit X3 3.0.9">
<link rel="up" href="../tutorials.html" title="Tutorials">
<link rel="prev" href="number_list_redux___list_syntax.html" title="Number List Redux - list syntax">
<link rel="next" href="roman.html" title="Roman Numerals">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="number_list_redux___list_syntax.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorials.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="roman.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="spirit_x3.tutorials.number_list_attribute___one_more__with_style"></a><a class="link" href="number_list_attribute___one_more__with_style.html" title="Number List Attribute - one more, with style">Number
      List Attribute - one more, with style</a>
</h3></div></div></div>
<p>
        You've seen that the <code class="computeroutput"><span class="identifier">double_</span></code>
        parser has a <code class="computeroutput"><span class="keyword">double</span></code> attribute.
        All parsers have an attribute, even complex parsers. Those that are composed
        from primitives using operators, like the list parser, also have an attribute.
        It so happens that the attribute of a list parser:
      </p>
<pre class="programlisting"><span class="identifier">p</span> <span class="special">%</span> <span class="identifier">d</span>
</pre>
<p>
        is a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span></code> of the attribute of <code class="computeroutput"><span class="identifier">p</span></code>. So, for our parser:
      </p>
<pre class="programlisting"><span class="identifier">double_</span> <span class="special">%</span> <span class="char">','</span>
</pre>
<p>
        we'll have an attribute of:
      </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span>
</pre>
<p>
        So, what does this give us? Well, we can simply pass in a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span></code> to our number list parser and it will
        happily churn out our result in our vector. For that to happen, we'll use
        a variation of the <code class="computeroutput"><span class="identifier">phrase_parse</span></code>
        with an additional argument: the parser's attribute. With the following arguments
        passed to <code class="computeroutput"><span class="identifier">phrase_parse</span></code>
      </p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
            An iterator pointing to the start of the input
          </li>
<li class="listitem">
            An iterator pointing to one past the end of the input
          </li>
<li class="listitem">
            The parser object
          </li>
<li class="listitem">
            Another parser called the skip parser
          </li>
<li class="listitem">
            The parser's attribute
          </li>
</ol></div>
<p>
        Our parser now is further simplified to:
      </p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Iterator</span><span class="special">&gt;</span>
<span class="keyword">bool</span> <span class="identifier">parse_numbers</span><span class="special">(</span><span class="identifier">Iterator</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">Iterator</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;&amp;</span> <span class="identifier">v</span><span class="special">)</span>
<span class="special">{</span>
    <span class="keyword">using</span> <span class="identifier">x3</span><span class="special">::</span><span class="identifier">double_</span><span class="special">;</span>
    <span class="keyword">using</span> <span class="identifier">x3</span><span class="special">::</span><span class="identifier">phrase_parse</span><span class="special">;</span>
    <span class="keyword">using</span> <span class="identifier">x3</span><span class="special">::</span><span class="identifier">_attr</span><span class="special">;</span>
    <span class="keyword">using</span> <span class="identifier">ascii</span><span class="special">::</span><span class="identifier">space</span><span class="special">;</span>

    <span class="keyword">bool</span> <span class="identifier">r</span> <span class="special">=</span> <span class="identifier">phrase_parse</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">,</span>

        <span class="comment">//  Begin grammar</span>
        <span class="special">(</span>
            <span class="identifier">double_</span> <span class="special">%</span> <span class="char">','</span>
        <span class="special">)</span>
        <span class="special">,</span>
        <span class="comment">//  End grammar</span>

        <span class="identifier">space</span><span class="special">,</span> <span class="identifier">v</span><span class="special">);</span>

    <span class="keyword">if</span> <span class="special">(</span><span class="identifier">first</span> <span class="special">!=</span> <span class="identifier">last</span><span class="special">)</span> <span class="comment">// fail if we did not get a full match</span>
        <span class="keyword">return</span> <span class="keyword">false</span><span class="special">;</span>
    <span class="keyword">return</span> <span class="identifier">r</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
        The full cpp file for this example can be found here: <a href="../../../../../example/x3/num_list/num_list4.cpp" target="_top">num_list4.cpp</a>
      </p>
<p>
        <span class="bold"><strong>Hey, no more actions!!!</strong></span> Now we're entering
        the realm of attribute grammars. Cool eh?
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2001-2018 Joel de Guzman,
      Hartmut Kaiser<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="number_list_redux___list_syntax.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorials.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="roman.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
